﻿package com.taobao.top  
{
	import com.adobe.serialization.json.JSON;
	import com.taobao.top.model.Album;
	import com.taobao.top.model.Picture;
	import com.taobao.top.model.User;
	import com.taobao.top.resultmodel.*;
	import com.taobao.top.converter.UserConverter;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.FileReference;
	
	/**
	 * 淘宝SNS类API
	 * @author Dada
	 */
	public class TOPSNS extends EventDispatcher
	{
		
		public function TOPSNS() 
		{
			
		}
		
		public function getAlbums(uid:int,start_row:int,count:int):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.users.get"),
				new TopRequestParam("uid",uid.toString()),
				new TopRequestParam("start_row",start_row.toString()),
				new TopRequestParam("count",start_row.toString())];
			topURLLdr.addEventListener(Event.COMPLETE,albumsGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function albumsGetHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var albumGetResult:AlbumGetResult = albumsGetJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function albumsGetJsonParser(jsonStr:String):AlbumGetResult
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				return objToAlbumsGetResult((JSON.decode(jsonStr) as Object).rsp);
			}
			return null;
		}
		
		private function objToAlbumsGetResult(albumsGetObj:Object):AlbumGetResult
		{
			var albumsGetResult:AlbumGetResult = new AlbumGetResult();
			albumsGetResult.totoalResults = albumsGetObj.totalResults;
			for each(var albumObj:Object in albumsGetObj.albums)
			{
				albumsGetResult.albums.push(objToAlbum(albumObj));
			}
			return albumsGetResult;
		}
		
		private function objToAlbum(albumObj:Object):Album
		{
			var album:Album = new Album();
			album.cover_url =  albumObj.cover_url;
			album.id =  albumObj.id;
			album.memo =  albumObj.memo;
			album.name =  albumObj.name;
			album.nick =  albumObj.nick;
			album.pic_count =  albumObj.pic_count;
			album.uid =  albumObj.uid;
			return album;
		}
		
		public function getPictures(uid:int,album_id:int,start_row:int,count:int):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.users.get"),
				new TopRequestParam("uid",uid.toString()),
				new TopRequestParam("album_id",album_id.toString()),
				new TopRequestParam("start_row",start_row.toString()),
				new TopRequestParam("count",start_row.toString())];
			topURLLdr.addEventListener(Event.COMPLETE,picturesGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function picturesGetHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var picturesGetResult:PicturesGetResult = pictureGetJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function pictureGetJsonParser(jsonStr:String):PicturesGetResult
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				return objToPicturesGetResult((JSON.decode(jsonStr) as Object).rsp);
			}
			return null;
		}
		
		private function objToPicturesGetResult(picturesGetObj:Object):PicturesGetResult
		{
			var picturesGetResult:PicturesGetResult = new PicturesGetResult();
			picturesGetResult.totoalResults = picturesGetObj.totoalResults;
			for each(var pictureObj:Picture in picturesGetObj.pictures)
			{
				picturesGetResult.pictures.push(pictureObj);
			}
			return picturesGetResult;
		}
		
		private function objToPicture(pictureObj:Object):void
		{
			var picture:Picture = new Picture;
			picture.created = TOPUtil.getCustomDate(pictureObj.created);
			picture.deleted = pictureObj.deleted;
			picture.modified = TOPUtil.getCustomDate(pictureObj.modified);
		}
		
		public function addPicture(fileRef:FileReference):void
		{
			
		}
		/**
		 * 使照片的状态变成被插件使用的状态 
		 * 
		 */		
		public function setPictureUsed(ids:Array):void
		{
			
		}
		
		public function addActivity():void
		{
			
		}
		
		public function sendSystemMessage():void
		{
			
		}
		
		public function sendMessage():void
		{
			
		}
		
		public function getFriends():void
		{
			
		}
		
		public function areFriends():void
		{
			
		}
		
		public function getUser():void
		{
			
		}
		
		public function consumeCoins(uid:int):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.sns.coins.sum")];
			topURLLdr.addEventListener(Event.COMPLETE,userGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function userGetHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var user:User = userGetJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function userGetJsonParser(jsonStr:String):User
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				//TODO:待修复
				return UserConverter.convert((JSON.decode(jsonStr) as Object).rsp);
			}
			return null;
		}
		
		public function getCoinSum():void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.sns.coins.sum")];
			topURLLdr.addEventListener(Event.COMPLETE,coinSumGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function coinSumGetHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var coinSum:int = coinSumGetJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function coinSumGetJsonParser(jsonStr:String):int
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				//TODO:待修复
				return (JSON.decode(jsonStr) as Object).rsp as int;
			}
			return 0;
		}
	}
	
}